public class test528_1 {
    public static void main(String[] args) {
        //线性查找
        int[] arr1 = {1,2,54,87,44,5,6,9,7,78,99,100};
        int target1 = 5 , i;
        for (i = 0; i < arr1.length; i++) {
            if ( arr1[i] == target1) {
                System.out.println("找到了："+ target1 + "， 索引为：" + i);
                break;
            }
        }
        if (i == arr1.length) {
            System.out.println("对不起，没有你所需要的元素。");
        }
        //O(n)

        //二分法(要求数组是有序的)/O(log2n)
        int[] arr2 = {1,3,5,7,12,34,56,78,79,89,99,100};
            int target2 = 34, head =  0, end = arr2.length-1, middle = (end + head) / 2;
        //利用二分法进行查找
        for (; head >= end; ) {
            if (target2 == arr2[middle]) {
                System.out.print("找到了:" + target2 + "，索引为：" + middle);
            }else if (target2 > arr2[middle]) {
                head = middle + 1;
            }else{
                end = middle - 1;
            }
        }
    }
}
